package com.example.testcustocontrol;

import com.example.testcustocontrol.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

// this NumericKeyboard works only for EditTexts which 
// have a tag with "usesNumericKeyboard" key and value of "true"
// use tag with "ignoreMeForNumericKeyboardTouchListener" key and 
// value "true" for controls you do not want to set its touchListener 
public class NumericKeyboard extends LinearLayout {

    private View mainView = null;
    private EditText currentEditText;
    private Button btn0;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btnBackSpace;
    private Button btnDeleteAll;

    public NumericKeyboard(Context context) {
        super(context);
    }

    public NumericKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void initialize(EditText text) {
        LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater
                .inflate(R.layout.view_numeric_keyboard, this);

        currentEditText = text;
        
        btn0 = (Button) view.findViewById(R.id.btn0);
        btn1 = (Button) view.findViewById(R.id.btn1);
        btn2 = (Button) view.findViewById(R.id.btn2);
        btn3 = (Button) view.findViewById(R.id.btn3);
        btn4 = (Button) view.findViewById(R.id.btn4);
        btn5 = (Button) view.findViewById(R.id.btn5);
        btn6 = (Button) view.findViewById(R.id.btn6);
        btn7 = (Button) view.findViewById(R.id.btn7);
        btn8 = (Button) view.findViewById(R.id.btn8);
        btn9 = (Button) view.findViewById(R.id.btn9);
        btnBackSpace = (Button) view.findViewById(R.id.btnBackSpace);
        btnDeleteAll = (Button) view.findViewById(R.id.btnDeleteAll);

        btn0.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "0");
            }
        });
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "1");
            }
        });
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "2");
            }
        });
        btn3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "3");
            }
        });
        btn4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "4");
            }
        });
        btn5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "5");
            }
        });
        btn6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "6");
            }
        });
        btn7.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "7");
            }
        });
        btn8.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "8");
            }
        });
        btn9.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "9");
            }
        });
        btnBackSpace.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "<");
            }
        });
        btnDeleteAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "D");
            }
        });

        /*if (mainView == null && !this.isInEditMode()) {
            setTouchListenerForChildViews();
            this.setVisibility(GONE);
        }
        */
    }

    protected void editCurrentText(View v, String character) {
        if (currentEditText != null) {
            if (character.compareTo("<") == 0) {
                String text = currentEditText.getText().toString();
                if (text.length() == 0) {
                } else if (text.length() == 1) {
                    currentEditText.setText("");
                } else {
                    text = text.substring(0, text.length() - 1);
                    currentEditText.setText(text);
                }
            } else if (character.compareTo("D") == 0) {
                currentEditText.setText("");
            } else {
                String text = currentEditText.getText().toString();
                text += character;
                currentEditText.setText(text);
            }
        }
    }

	
}
